package com.yzz.composeweatherproject.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.yzz.composeweatherproject.IDataSuccessListener
import com.yzz.composeweatherproject.entity.LocationBean
import com.yzz.composeweatherproject.entity.WeatherDetailedBean
import com.yzz.composeweatherproject.entity.WeatherProvinceBean
import com.yzz.composeweatherproject.http.okhttp.OkHttpUtils
import com.yzz.composeweatherproject.http.retrofit2.RetrofitHttpUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class DataViewModel : ViewModel() {

    var dataStringFlow: MutableStateFlow<ArrayList<WeatherProvinceBean?>?> = MutableStateFlow(null)
    var detailedDataFlow: MutableStateFlow<WeatherDetailedBean?> = MutableStateFlow(null)

    private val locationWeatherFlow : MutableStateFlow<LocationBean?> by lazy {
        MutableStateFlow(null)
    }
    val mLocationWeatherFlow : StateFlow<LocationBean?> = locationWeatherFlow

    fun getShoppingData() {
        Thread {
            RetrofitHttpUtils.getInstacne()
                .getShoppingData("鞋子", object : IDataSuccessListener<String?> {
                    override fun onSuccess(data: String?) {
                        if (data != null) {
//                            dataStringFlow.value = data
                        }
                    }

                    override fun onFail(data: String?) {
//                        dataStringFlow.value = data.toString()

                    }

                })
        }.start()

    }

    fun getWeatherData() {
        viewModelScope.launch(Dispatchers.IO) {
            RetrofitHttpUtils.getInstacne()
                .getWeather(object : IDataSuccessListener<ArrayList<WeatherProvinceBean?>> {
                    override fun onSuccess(data: ArrayList<WeatherProvinceBean?>) {
                        dataStringFlow.value = data

                    }

                    override fun onFail(string: String?) {
                        dataStringFlow.value = null
                    }

                })
        }

    }

    fun getWeatherDetailed(url: String?){
        viewModelScope.launch(Dispatchers.IO) {
            OkHttpUtils.instance.getWeatherUrl(url, object : IDataSuccessListener<WeatherDetailedBean> {
                override fun onSuccess(data: WeatherDetailedBean) {
                    detailedDataFlow.value = data
                }

                override fun onFail(msg: String?) {

                }
            })
        }
    }

    fun getLocationData(){
        viewModelScope.launch(Dispatchers.IO) {
            OkHttpUtils.instance.getLocationUrl(object : IDataSuccessListener<LocationBean> {
                override fun onSuccess(data: LocationBean?) {
                    if (data != null){
                        locationWeatherFlow.value = data
                    }else{
                        locationWeatherFlow.value = null
                    }
                }

                override fun onFail(msg: String?) {
                    locationWeatherFlow.value = null
                }

            })
        }
    }

}